/*
 * @Author: your name
 * @Date: 2021-07-25 18:09:22
 * @LastEditTime: 2021-07-26 15:38:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \FaceRecognition\source\my_csv.cpp
 */

#include "iostream"
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <set>

#include "opencv2/core.hpp"
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "my_fileIO.h"
#include "student.h"


void read_csv(const string& filename, vector<Mat>& images, vector<student>& stdts, char separator) {
    std::ifstream file(filename.c_str(), ifstream::in);
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(Error::StsBadArg, error_message);
    }
    string line, path, num, name;
    student sd;
	set<string> m;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, num, separator);
        getline(liness, name);
		if (m.find(num) != m.end()) {
			continue;
		}
		m.insert(num);
        //cout <<"-----"<<path<<"*****"<<num<<":"<<name<<endl;
        if(!path.empty() && !num.empty()) {
            sd.num = atoi(num.c_str());
            strcpy(sd.name, name.c_str());
			Mat img = imread(path, 0);
			if (img.empty()) {
				continue;
			}
            images.push_back(img);
            stdts.push_back(sd);
        }
    }
    file.close();
}

void write_csv(const string& filename, string imgdir, student sd, char separator) {
    std::ofstream file(filename.c_str(), ofstream::app);
    if (!file) {
        string error_message = "No valid output file was given, please check the given filename.";
        CV_Error(Error::StsBadArg, error_message);
    }
    file << format("\n%s%c%d%c%s", imgdir.c_str(), separator, sd.num, separator, sd.name);
    file.close();
}



void Getfilepath(const char *path, const char *filename,  char *filepath)
{
    strcpy(filepath, path);
    if(filepath[strlen(path) - 1] != '/')
        strcat(filepath, "/");
    strcat(filepath, filename);
	//printf("path is = %s\n",filepath);
}
 
bool DeleteFile(const char* path)
{
    DIR *dir;
    struct dirent *dirinfo;
    struct stat statbuf;
    char filepath[256] = {0};
    lstat(path, &statbuf);
    
    if (S_ISREG(statbuf.st_mode))//判断是否是常规文件
    {
        remove(path);
    }
    else if (S_ISDIR(statbuf.st_mode))//判断是否是目录
    {
        if ((dir = opendir(path)) == NULL)
            return 1;
        while ((dirinfo = readdir(dir)) != NULL)
        {
            Getfilepath(path, dirinfo->d_name, filepath);
            if (strcmp(dirinfo->d_name, ".") == 0 || strcmp(dirinfo->d_name, "..") == 0)//判断是否是特殊目录
            continue;
            DeleteFile(filepath);
			printf("path is = %s\n",filepath);
            rmdir(filepath);
        }
        closedir(dir);
    }
    return 0;
}


void delete_face_by_label(const string& filename, int label, char separator) {
    std::fstream file(filename.c_str(), ios::out | ios::in);
    if (!file) {
        string error_message = "No valid output file was given, please check the given filename.";
        CV_Error(Error::StsBadArg, error_message);
    }
    string line, lines, path, num, name;
    int flag = 0;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, num, separator);
        getline(liness, name);
        if (atoi(num.c_str()) == label) {
            DeleteFile(format("../data/face%d", label).c_str());
            flag = 1;
            continue;
        }
        lines += line + '\n';
    }
    if (1 == flag) {
        cout << "successful delete!" << endl;
        //cout << lines << endl;
    }
    file.close();
    file.open(filename.c_str(), ios::out | ios::trunc);
    file << lines;
    file.close();
}
